package com.example.elysiumease.model;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Getter
@Setter
@Table(name = "soul_sins")
@IdClass(SoulSin.SoulSinId.class)
public class SoulSin {

    @Id
    @ManyToOne
    @JoinColumn(name = "soul_id", nullable = false)
    private Soul soul;

    @Id
    @ManyToOne
    @JoinColumn(name = "sin_id", nullable = false)
    private Sin sin;

    @Column(name = "occurrence_count", nullable = false)
    private Short occurrenceCount = 1;




    // 显式实现 equals 和 hashCode
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SoulSin)) return false;
        SoulSin soulSin = (SoulSin) o;
        return Objects.equals(soul.getSoulId(), soulSin.soul.getSoulId()) &&
                Objects.equals(sin.getSinId(), soulSin.sin.getSinId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(soul.getSoulId(), sin.getSinId());
    }

    // 联合主键类
    public static class SoulSinId implements Serializable {
        private Long soul; // 假设 Soul 的主键是 Long
        private Short sin;

        public SoulSinId() {}

        public SoulSinId(Long soul, Short sin) {
            this.soul = soul;
            this.sin = sin;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            SoulSinId soulSinId = (SoulSinId) o;
            return Objects.equals(soul, soulSinId.soul) &&
                    Objects.equals(sin, soulSinId.sin);
        }

        @Override
        public int hashCode() {
            return Objects.hash(soul, sin);
        }

        // Getters and Setters
        public Long getSoul() { return soul; }
        public void setSoul(Long soul) { this.soul = soul; }
        public Short getSin() { return sin; }
        public void setSin(Short sin) { this.sin = sin; }
    }
}